package cn.sinata.wanglin.mine.activity;
import android.support.v7.widget.RecyclerView;
import com.xilada.xldutils.activitys.RecyclerListActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import cn.sinata.wanglin.adapter.MyLeadMaterialAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.MyLeadMaterial;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;


/**
 * 小区生活---我的领物
 */

public class MyleadmaterialActivity extends RecyclerListActivity {
    private MyLeadMaterialAdapter mMyleadmaterialAdapter;
    private List<MyLeadMaterial> mDatas=new ArrayList<MyLeadMaterial>();
    private int pageNo = 0;
    @Override
    protected void initView() {
        super.initView();
        setTitle("我的领物");
        showDialog();
        getData();
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        mMyleadmaterialAdapter=new MyLeadMaterialAdapter(mDatas);
        return mMyleadmaterialAdapter;
    }


    @Override
    protected void pullDownRefresh() {
        pageNo = 0;
        getData();
    }

    @Override
    protected void loadMore() {
        pageNo++;
        getData();
    }
    void getData(){
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getDepositList(userid,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<MyLeadMaterial>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<MyLeadMaterial> datas) {
                        if (pageNo == 0) {
                            mDatas.clear();
                        }
                        if (datas.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mDatas.addAll(datas);
                        Comparator<MyLeadMaterial> materialComparator=new Comparator<MyLeadMaterial>() {
                            @Override
                            public int compare(MyLeadMaterial myLeadMaterial, MyLeadMaterial t1) {
                                if(Integer.parseInt(myLeadMaterial.getStatus())==1){
                                    return 1;
                                }else{
                                    return -1;
                                }
                            }
                        };
                        Collections.sort(mDatas,materialComparator);
                        mMyleadmaterialAdapter.notifyDataSetChanged();
                    }
                });

    }
}